package com.huatai.common.httputil;

/**
 * @program: ht-system
 * @description:
 * @author: songxiude
 * @create: 2023-04-25 15:30
 **/
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

public class Exceptions {
	public Exceptions() {
	}

	public static RuntimeException unchecked(Throwable e) {
		if (e instanceof Error) {
			throw (Error)e;
		} else if (!(e instanceof IllegalAccessException) && !(e instanceof IllegalArgumentException) && !(e instanceof NoSuchMethodException)) {
			if (e instanceof InvocationTargetException) {
				return new RuntimeException(((InvocationTargetException)e).getTargetException());
			} else if (e instanceof RuntimeException) {
				return (RuntimeException)e;
			} else {
				if (e instanceof InterruptedException) {
					Thread.currentThread().interrupt();
				}
				return runtime(e);
				//return (RuntimeException)runtime(e);
			}
		} else {
			return new IllegalArgumentException(e);
		}
	}

	private static <T extends Throwable> T runtime(Throwable throwable) throws T {
		try {
			throw throwable;
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
//		try {
//			throw throwable;
//		} catch (Throwable e) {
//			throw new RuntimeException(e);
//		}
	}

	public static Throwable unwrap(Throwable wrapped) {
		Throwable unwrapped = wrapped;

		while(true) {
			while(!(unwrapped instanceof InvocationTargetException)) {
				if (!(unwrapped instanceof UndeclaredThrowableException)) {
					return unwrapped;
				}

				unwrapped = ((UndeclaredThrowableException)unwrapped).getUndeclaredThrowable();
			}

			unwrapped = ((InvocationTargetException)unwrapped).getTargetException();
		}
	}

	public static String getStackTraceAsString(Throwable ex) {
		FastStringWriter stringWriter = new FastStringWriter();
		ex.printStackTrace(new PrintWriter(stringWriter));
		return stringWriter.toString();
	}
}

